package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.bac.BacWorkFlowNode;
import com.ysd.lis.mapper.bac.BacWorkFlowNodeMapper;
import com.ysd.lis.request.bac.BacFlowNodeDto;
import com.ysd.lis.service.bac.BacWorkFlowNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 流程设定主表 服务实现类
 * </p>
 *
 * @author cfz
 * @since 2024-04-09
 */
@Service
public class BacWorkFlowNodeServiceImpl extends ServiceImpl<BacWorkFlowNodeMapper, BacWorkFlowNode> implements BacWorkFlowNodeService {
    @Autowired
    BacWorkFlowNodeMapper bacWorkFlowNodeMapper;

    @Override
    public Result findBacWorkFlowNodeList(BacWorkFlowNode bacWorkFlowNode) {
        List<BacWorkFlowNode> list = findBacWorkFlowNodeArray(bacWorkFlowNode);
        return Result.succ(1, "请求成功", list);
    }

    @Override
    public List<BacWorkFlowNode> findBacWorkFlowNodeArray(BacWorkFlowNode bacWorkFlowNode) {
        MPJLambdaWrapper<BacWorkFlowNode> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(BacWorkFlowNode::getDelFlag, 0);
        queryWrapper.eq(StringUtils.isNotBlank(bacWorkFlowNode.getId()), BacWorkFlowNode::getId, bacWorkFlowNode.getId());
        queryWrapper.eq(StringUtils.isNotBlank(bacWorkFlowNode.getFlowNo()), BacWorkFlowNode::getFlowNo, bacWorkFlowNode.getFlowNo());
        return bacWorkFlowNodeMapper.selectList(queryWrapper);
    }

    @Override
    public Result addBacWorkFlowNode(BacWorkFlowNode bacWorkFlowNode) {
        int i = bacWorkFlowNodeMapper.insert(bacWorkFlowNode);
        if (i > 0) {
            return Result.succ(1, "新增成功", bacWorkFlowNode);
        }
        return Result.fail("新增失败");
    }

    @Override
    public Result deleteBacWorkFlowNode(BacWorkFlowNode bacWorkFlowNode) {
        LambdaQueryWrapper<BacWorkFlowNode> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BacWorkFlowNode::getFlowNo,bacWorkFlowNode.getFlowNo());
        lambdaQueryWrapper.eq(BacWorkFlowNode::getNodeDictNo,bacWorkFlowNode.getNodeDictNo());
        int i = bacWorkFlowNodeMapper.delete(lambdaQueryWrapper);
        if (i > 0) {
            return Result.succ(1, "删除成功", bacWorkFlowNode);
        }
        return Result.fail("删除失败");
    }

    @Override
    public Result updateBacWorkFlowNode(BacWorkFlowNode bacWorkFlowNode) {
        int i = bacWorkFlowNodeMapper.updateById(bacWorkFlowNode);
        if (i > 0) {
            return Result.succ(1, "更新成功", bacWorkFlowNode);
        }
        return Result.fail("更新失败");
    }

    @Override
    public Result saveOrUpdateBacWorkFlowNode(BacFlowNodeDto dto) {
        List<BacWorkFlowNode> bacWorkFlowNodeList = dto.getBacWorkFlowNodeList();

        boolean b = this.updateBatchById(bacWorkFlowNodeList);
        if (b) {
            return Result.succ(1, "成功", null);
        } else {
            return Result.fail(400, "失败", "");
        }
    }
}
